<template>
    <div class="video-page">
        <div class="video-container">
            <el-tabs v-model="activeName" :tab-position="tabPosition" @tab-change="changeVideo">
                <el-tab-pane
                    v-for="(info, index) in videoInfos"
                    :key="index"
                    :label="info.label"
                    :name="index"
                >
                    <video
                        type="video/mp4"
                        :ref="
                            (el) => {
                                if (el) {
                                    videoRefs[index] = el
                                }
                            }
                        "
                        :src="videoSrc"
                        :controls="!isMobile"
                        @play="onPlay"
                        @pause="onPause"
                        @mousewheel="mouseWheelHandle"
                        @DOMMouseScroll="mouseWheelHandle"
                    >
                        <!-- <source :src="videoSrc" type="video/mp4" /> -->
                        您的浏览器不支持HTML5视频。
                    </video>
                    <div v-if="isPaused" class="pause-icon">
                        <el-icon size="100" color="#cbc4c1">
                            <inconify icon="ep:caret-right" />
                        </el-icon>
                    </div>
                    <div v-if="!isMobile" class="video-tool-container">
                        <el-icon @click="to_previous" class="arrow-up" size="30" color="#909399">
                            <inconify icon="ep:arrow-up" />
                        </el-icon>
                        <el-icon @click="to_next" class="arrow-down" size="30" color="#909399">
                            <inconify icon="ep:arrow-down" />
                        </el-icon>
                    </div>
                </el-tab-pane>
            </el-tabs>
        </div>
    </div>
</template>

<script setup name="video">
import { Icon as inconify } from '@iconify/vue'
import { ref, reactive, computed, watchEffect, onMounted, onUnmounted } from 'vue'
//注意:浏览器限制进入后如果不开启静音不允许自动播放
const videoInfos = reactive([
    { label: '女大学生', url: 'https://api.yujn.cn/api/nvda.php?type=video' },
    { label: '黑丝系列', url: 'http://api.yujn.cn/api/heisis.php?type=video' },
    { label: '慢摇系列', url: 'http://api.yujn.cn/api/manyao.php?type=video' },
    { label: '吊带系列', url: 'http://api.yujn.cn/api/diaodai.php?type=video' },
    { label: '清纯系列', url: 'http://api.yujn.cn/api/qingchun.php?type=video' },
    { label: '女高系列', url: 'http://api.yujn.cn/api/nvgao.php?type=video' },
    { label: '甜妹系列', url: 'http://api.yujn.cn/api/tianmei.php?type=video' },
    { label: '随机妹子1', url: 'http://api.yujn.cn/api/zzxjj.php?type=video' },
    {
        label: '随机妹子2',
        url: 'https://api.linhun.vip/api/Littlesistervideo?type=video&apiKey=48b0b2ef0f8483326c3c5424911d5924'
    }
])

const currentApiIndex = ref(0)

const currentVideoIndex = ref(0)

const videoSrc = computed(() => {
    if (!videoInfos[currentApiIndex.value]) return ''
    return `${videoInfos[currentApiIndex.value].url}&index=${currentVideoIndex.value}`
})
//获取播放器实例
const videoRefs = reactive([])
const activeName = ref(0)
const isPaused = ref(false)
const tabPosition = ref('left')
const isScrolling = ref(false) // 是否在滚动,是为了防止滚动多页，需要通过一个变量来控制是否滚动
/* 媒体查询：屏幕宽度小于1024px  返回MediaQueryList的matches属性，它表示媒体查询是否匹配 */
const mediaQuery = window.matchMedia('(max-width: 1023px)')
const isMobile = ref(mediaQuery.matches)
// 监听媒体查询的变化
mediaQuery.addEventListener('change', (e) => {
    // 当媒体查询匹配状态变化时，更新 isMobile 的值
    isMobile.value = e.matches
})
watchEffect(() => {
    if (isMobile.value) {
        tabPosition.value = 'top'
    } else {
        tabPosition.value = 'left'
    }
})
function onPlay() {
    isPaused.value = false
}
function onPause() {
    isPaused.value = true
}
// 当标签页切换时，暂停非活动的视频
function pausedHandle(activeTabName) {
    videoRefs.forEach((video, index) => {
        if (index !== parseInt(activeTabName)) {
            if (!video.paused) {
                video.pause()
            }
        }
    })
}
/**
 *
 * @param tab_pane 当前tab的name 此处赋值为索引
 */
function changeVideo(tab_pane) {
    pausedHandle(tab_pane)
    currentApiIndex.value = tab_pane
    currentVideoIndex.value = 0
    // 触发视频源的更新
    updateVideoSource()
}

function to_previous() {
    //暂停在播放时视频
    pausedHandle(currentApiIndex.value)
    if (currentVideoIndex.value > 0) {
        currentVideoIndex.value--
        // 触发视频源的更新
        updateVideoSource()
    }
}
function to_next() {
    //暂停在播放时视频
    pausedHandle(currentApiIndex.value)
    currentVideoIndex.value++
    // 触发视频源的更新
    updateVideoSource()
}
// 触发视频源更新
function updateVideoSource() {
    // 更新视频播放器的 src 属性
    videoRefs[currentApiIndex.value].src = videoSrc.value
    // 自动播放视频
    videoRefs[currentApiIndex.value].autoplay = true
}

// 处理鼠标滚轮事件
function mouseWheelHandle(event) {
    // 添加冒泡阻止
    const e = event.originalEvent || event
    if (e.stopPropagation) {
        e.stopPropagation()
    } else {
        e.returnValue = false
    }
    if (isScrolling.value) {
        // 判断是否可以滚动
        return false
    }
    const deltaY = e.deltaY || e.detail // Firefox 使用 detail
    if (deltaY < 0) {
        // 向上滚动
        to_previous()
    } else {
        // 向下滚动
        to_next()
    }
}
// 切换播放/暂停
function togglePlayPause() {
    if (videoRefs[currentApiIndex.value].paused) {
        videoRefs[currentApiIndex.value].play()
    } else {
        videoRefs[currentApiIndex.value].pause()
    }
}
// 使用触摸事件
function handleTouchStart(event) {
    togglePlayPause(event)
}

// 在组件挂载时添加事件监听器
onMounted(() => {
    document.addEventListener('wheel', mouseWheelHandle, { passive: true })
    document.addEventListener('touchstart', handleTouchStart, { passive: false })
})
// 在组件卸载时移除事件监听器
onUnmounted(() => {
    document.removeEventListener('wheel', mouseWheelHandle)
    document.removeEventListener('touchstart', handleTouchStart)
})
</script>
<style lang="scss" scoped>
.video-page {
    background-color: black;
    & .video-container {
        transition: all linear 0.5s;
        & .el-tabs {
            & .el-tabs__header {
                /* 清除默认样式 */
                margin: 0px;
                & .is-active {
                    color: #fff;
                }
                & .el-tabs__item {
                    &:hover {
                        color: #fff; // 悬停时的背景色
                    }
                }
                & .el-tabs__active-bar {
                    //display: none;
                    background-color: var(--el-text-color-primary);
                }
            }

            & .el-tabs__content {
                & .el-tab-pane {
                    @include flex-center;
                    & .arrow-up,
                    .arrow-down {
                        display: block;
                        &:hover {
                            color: #fff; // 悬停时的背景色
                        }
                    }
                    & video {
                        cursor: pointer; // 设置整个页面的鼠标样式为手指样式
                        width: calc(100% - 40px);
                        height: calc(100vh - 90px);
                    }
                    & .video-tool-container {
                        margin-left: 5px;
                        margin-right: 5px;
                        display: inline;
                        width: 30px;
                        background-color: #606266;
                        border-radius: 15px;
                    }
                    & .pause-icon {
                        position: absolute;
                        top: 50%;
                        left: 50%;
                        transform: translate(-50%, -50%);
                        z-index: 1Q;
                        pointer-events: none; /* 确保点击图标不会影响视频 */
                    }
                }
            }
        }
    }
}

/* 媒体查询：屏幕宽度小于1024px */
@media (max-width: 1023px) {
    .video-page {
        //background-color: transparent;
        // @include border($border-color: green);
        & .video-container {
            & .el-tabs {
                & .el-tabs__content {
                    & .el-tab-pane {
                        & video {
                            width: 100%;
                            height: 100vh;
                            object-fit: fill; /* 填充整个容器 */
                        }
                    }
                }
            }
        }
    }
}
</style>
